package com.sxu.demo;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * invokeAll
 */
public class Test4 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(2);

        List<Future<String>> futures = pool.invokeAll(Arrays.asList(
                () -> {
                    System.out.println("Begin...");
                    System.out.println(LocalTime.now());
                    Thread.sleep(5000);
                    return "1";
                },
                () -> {
                    System.out.println("Begin...");
                    System.out.println(LocalTime.now());
                    Thread.sleep(5000);
                    return "2";
                },
                () -> {
                    System.out.println("Begin...");
                    System.out.println(LocalTime.now());
                    Thread.sleep(5000);
                    return "3";
                }
        ));

        futures.forEach(future -> {
            try {
                System.out.println(future.get());
                System.out.println(LocalTime.now());
            } catch (InterruptedException| ExecutionException e) {
                throw new RuntimeException(e);
            }
        });

    }
}
